﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Crolengi, this program (a part of program) is gameengine based on C++\Directx
//    Copyright (C) 2008 Pashinin Anton Alekseevich (Crol)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//    (INCLUDING NEGLIGENCE OR OTHERWISE) 
//    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//    Contacts: 
//    Mail: crolengi@gmail.com
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef  light_h
#define  light_h

#include <d3d9.h>
#include <d3dx9.h>

#include "..\\import\\CRLEngi_strings_lib\\CRLEngi_strings.h"
#include "..\\import\\param_trees_lib\\param_trees.h"

#include "include\\Utils\\DrawingFunctions.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
/////   :: КЛАСС ИСТОЧНИКА СВЕТА ::
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CLREngi_Light{ 
public:
	CRLEngi_string  LightName;

	D3DXVECTOR4     Position,Look;
	
	D3DXMATRIX      LightMatrix;
	D3DXMATRIX      LightView;
	D3DXMATRIX      LightProjection;

	float   Falloff;
	float    Specular;
	float    Specular_Itens;
	float    Ambient;
	float    Attenuation;
	D3DXVECTOR4     Color;
//#################################################################################################################
public:
bool	Light_setup(CRLEngi_string Name,DynamicRow *row){
        LightName = Name;
		int id1,id2,id3,count;
		id1 = row->iLookId(LightName);
		count =  row->iCountKnots(id1) + row->iCountParams(id1);
		if (count>0){

		id2 = row->iLookInFor(id1,"_POSITION_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=3){
			id2 = row->TopId(id2);
			Position.x = (row->iLookName(id2)).toFloat();
			id2 = row->NextId(id2);
			Position.y = (row->iLookName(id2)).toFloat();
			id2 = row->NextId(id2);
			Position.z = (row->iLookName(id2)).toFloat();	
			Position.w = 1.0f;
		}

		id2 = row->iLookInFor(id1,"_LOOK_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=3){
			id2 = row->TopId(id2);
			Look.x = (row->iLookName(id2)).toFloat();
			id2 = row->NextId(id2);
			Look.y = (row->iLookName(id2)).toFloat();
			id2 = row->NextId(id2);
			Look.z = (row->iLookName(id2)).toFloat();	
			Look.w = 1.0f;
		}

		id2 = row->iLookInFor(id1,"_COLOR_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=3){
			id2 = row->TopId(id2);
			Color.x = (row->iLookName(id2)).toFloat();
			id2 = row->NextId(id2);
			Color.y = (row->iLookName(id2)).toFloat();
			id2 = row->NextId(id2);
			Color.z = (row->iLookName(id2)).toFloat();	
			Color.w = 1.0f;
		}

		id2 = row->iLookInFor(id1,"_FALLOFF_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=1){
			id2 = row->TopId(id2);
			Falloff = (row->iLookName(id2)).toFloat();
		}

		id2 = row->iLookInFor(id1,"_SPECULAR_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=1){
			id2 = row->TopId(id2);
			Specular = (row->iLookName(id2)).toFloat();
		}

		id2 = row->iLookInFor(id1,"_SPECULAR_INTENSY_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=1){
			id2 = row->TopId(id2);
			Specular_Itens = (row->iLookName(id2)).toFloat();
		}

		id2 = row->iLookInFor(id1,"_AMBIENT_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=1){
			id2 = row->TopId(id2);
			Ambient = (row->iLookName(id2)).toFloat();
		}

		id2 = row->iLookInFor(id1,"_ATTENUATION_");
		count =  row->iCountKnots(id2) + row->iCountParams(id2);
		if (count>=1){
			id2 = row->TopId(id2);
			Attenuation = (row->iLookName(id2)).toFloat();
		}
		return true;
		}
		else return false;			
	
}
void    Get_PrimitiveDrawing(LPDIRECT3DDEVICE9      *Device,
							 LPDIRECT3DTEXTURE9     *Texture1, 
	                         LPDIRECT3DTEXTURE9     *Texture2 ){

	D3DXVECTOR3 v1,v2,v3,v4,v5,v6,v7,v8,v9,v0;
	v1.x = Position.x+15;
	v1.y = Position.y+15;
	v1.z = Position.z+15;

	v2.x = Position.x-15;
	v2.y = Position.y-15;
	v2.z = Position.z-15;

	v3.x = Position.x+15;
	v3.y = Position.y+15;
	v3.z = Position.z-15;

	v4.x = Position.x-15;
	v4.y = Position.y-15;
	v4.z = Position.z+15;

	v5.x = Position.x+15;
	v5.y = Position.y-15;
	v5.z = Position.z-15;

	v6.x = Position.x-15;
	v6.y = Position.y+15;
	v6.z = Position.z+15;

	v7.x = Position.x-15;
	v7.y = Position.y+15;
	v7.z = Position.z-15;

	v8.x = Position.x+15;
	v8.y = Position.y-15;
	v8.z = Position.z+15;

	v9.x = Position.x;
	v9.y = Position.y-20;
	v9.z = Position.z;

	v0.x = Position.x;
	v0.y = Position.y+20;
	v0.z = Position.z;
	
	(*Device)->SetTexture(0,*Texture1);
	drawLine(v1,v2,Device);
	drawLine(v3,v4,Device);
	drawLine(v5,v6,Device);
	drawLine(v7,v8,Device);
	drawLine(v9,v0,Device);
	(*Device)->SetTexture(0,*Texture2);

	drawBox(v1,v2,Device);

	drawLine(v9,v5,Device);
	drawLine(v9,v2,Device);
	drawLine(v9,v8,Device);
	drawLine(v9,v4,Device);

	drawLine(v0,v1,Device);
	drawLine(v0,v3,Device);
	drawLine(v0,v6,Device);
	drawLine(v0,v7,Device);
}
//#################################################################################################################
public:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                     КОНСТРУКТОР
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	CLREngi_Light(){ 
		Position = D3DXVECTOR4(0,0,0,0);
		Look     = D3DXVECTOR4(0,0,0,0);
		Color    = D3DXVECTOR4(1,1,1,1);

		Falloff       =1000.0f;
		Specular       =8.5f ;
		Specular_Itens       =0.19f;
		Ambient           = 0.005f ;
		Attenuation       = 0.8f ;

	};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                       ДЕСТРУКТОР
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	virtual ~CLREngi_Light(){
	};

};

#endif 